package com.question.dicegame;

import java.awt.Graphics;
import java.awt.Image;

public class Player {

	private Image image;// 표시될 이미지
	private int x, y;// 위치 정보
	private int dx, dy = 0;// 이동량

	private int sliceCount1 = 4;
	private int sliceCount2 = 5;
	private int imageWidth;
	private int imageHeight = 80;
	private int currentImagePosition;
	private int direction;// 0:up, 1:down, 3:right, 4, left
	private long speed = 100;
	private long lastAnimatedTime = System.currentTimeMillis();

	public Player(Image image, int sliceCount) {
		this.image = image;
		y = 240;
		x = 800;

		this.imageWidth = image.getWidth(null) / sliceCount;
		this.currentImagePosition = 0;
		this.direction = 2;
	}

	public void draw(Graphics g, int number, int flag) {

		// g.drawImage(image, x, y,null);

		int sx = imageWidth * currentImagePosition;
		int sy = imageHeight * direction;

		g.drawImage(image, x, // 이미지를 그릴 left 좌표
				y, // 이미지를 그릴 top 좌표
				x + imageWidth, // 이미지를 그릴 right 좌표
				y + imageHeight, // 이미지를 그릴 bottom 좌표
				sx, // 원본 이미지에서 추출할 left 좌표
				sy, // 원본 이미지에서 추출할 top 좌표
				sx + imageWidth, // 원본 이미지에서 추출할 right 좌표
				sy + imageHeight, // 원본 이미지에서 추출할 bottom 좌표
				null);

		if (number != 0) {
			long current = System.currentTimeMillis();
			if (current - lastAnimatedTime > speed) {
				lastAnimatedTime = current;
				currentImagePosition++;

				if (number == 0 || dx != 0
						|| currentImagePosition >= sliceCount1)
					currentImagePosition = 0;
				if (number == 0 || dy != 0
						|| currentImagePosition >= sliceCount2)
					currentImagePosition = 0;
			}
		}
		if (flag == 1)
			currentImagePosition = 0;
	}

	public int movePaint(int number,int a) {
		if ((x == 10 * imageWidth || x == 11 * imageWidth)
				&& (y == 3 * imageWidth || y == 4 * imageWidth)) {
			x = -70;
			y = 0;
		} else if (x < 9 * imageWidth && y == 0 * imageWidth) {
			x += 10;
			direction = 2;
		} else if (x == 9 * imageWidth && y < 1 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x > 0 * imageWidth && y == 1 * imageWidth) {
			x -= 10;
			direction = 1;
		} else if (x == 0 * imageWidth && y < 2 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x < 9 * imageWidth && y == 2 * imageWidth) {
			x += 10;
			direction = 2;
		} else if (x == 9 * imageWidth && y < 3 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x > 0 * imageWidth && y == 3 * imageWidth) {
			x -= 10;
			direction = 1;
		} else if (x == 0 * imageWidth && y < 4 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x < 9 * imageWidth && y == 4 * imageWidth) {
			x += 10;
			direction = 2;
		} else if (x == 9 * imageWidth && y < 5 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x > 0 * imageWidth && y == 5 * imageWidth) {
			x -= 10;
			direction = 1;
		} else if (x == 0 * imageWidth && y < 6 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x < 9 * imageWidth && y == 6 * imageWidth) {
			x += 10;
			direction = 2;
		} else if (x == 9 * imageWidth && y < 7 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x > 0 * imageWidth && y == 7 * imageWidth) {
			x -= 10;
			direction = 1;
		} else if (x == 0 * imageWidth && y < 8 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x < 9 * imageWidth && y == 8 * imageWidth) {
			x += 10;
			direction = 2;
		} else if (x == 9 * imageWidth && y < 9 * imageWidth) {
			y += 10;
			direction = 0;
		} else if (x > 0 * imageWidth && y == 9 * imageWidth) {
			x -= 10;
			direction = 1;
			/*
			 * // 주사위가 이동량보다 많을 때 뒤로 가는 상황 for (int i = 1; i < 7; i++) { if (x <
			 * 5 * imageWidth && (number) < i) { switch (number) { case 5: x = 1
			 * * imageWidth; break; case 3: x = 2 * imageWidth; break;
			 * 
			 * case 2: x = 3 * imageWidth; break;
			 * 
			 * case 1: x = 4 * imageWidth; break;
			 * 
			 * } } else if (x == 5 * imageWidth && (number) == 6) { x = 0; x +=
			 * 0 * imageWidth; } }
			 */
		} else if (x == 0 * imageWidth && y == 9 * imageWidth) {
			
			return a = 1;
		}
		return a = 0;
	}

	public boolean moveEvent() {
		// 사다리 이벤트

		boolean running = true;
		if (x == 5 * imageWidth && y == 0 * imageWidth) {
			x = 8 * imageWidth;
			y = 4 * imageWidth;
		} else if (x == 9 * imageWidth && y == 1 * imageWidth) {
			x = 9 * imageWidth;
			y = 2 * imageWidth;
		} else if (x == 4 * imageWidth && y == 2 * imageWidth) {
			x = 2 * imageWidth;
			y = 7 * imageWidth;
		} else if (x == 1 * imageWidth && y == 4 * imageWidth) {
			x = 0 * imageWidth;
			y = 5 * imageWidth;
		} else if (x == 9 * imageWidth && y == 5 * imageWidth) {
			x = 9 * imageWidth;
			y = 8 * imageWidth;
		} else if (x == 8 * imageWidth && y == 9 * imageWidth) {
			x = 5 * imageWidth;
			y = 9 * imageWidth;
		}
		// 꼬리 이벤트
		if (x == 2 * imageWidth && y == 9 * imageWidth) {
			x = 2 * imageWidth;
			y = 1 * imageWidth;
		} else if (x == 3 * imageWidth && y == 7 * imageWidth) {
			x = 8 * imageWidth;
			y = 1 * imageWidth;
		} else if (x == 8 * imageWidth && y == 7 * imageWidth) {
			x = 4 * imageWidth;
			y = 3 * imageWidth;
		}
		if (x == 0 * imageWidth && y == 9 * imageWidth) {
			running = false;
		}

		return running;
	}
	
	public void backPaint() {
		if(x <= 9 * imageWidth && y == 9 * imageWidth){
			x += 10;
			direction = 2;
		}
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return image.getWidth(null);
	}

	public int getHeight() {
		return image.getHeight(null);
	}

	public int getDx() {
		return dx;
	}

	public void setDx(int dx) {
		this.dx = dx;
	}

	public int getDy() {
		return dy;
	}

	public void setDy(int dy) {
		this.dy = dy;
	}

}
